import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:toolkit/components/toast.dart';
import 'package:toolkit/global.dart';
import 'package:toolkit/iconfont.dart';
import 'package:toolkit/utils/encrypt.dart';

class TextAESPage extends StatefulWidget {
  TextAESPage({Key key}) : super(key: key);
  @override
  _TextAESPageState createState() => _TextAESPageState();
}

class _TextAESPageState extends State<TextAESPage> {
  @override
  void initState() {
    super.initState();
  }

  String _originText = '';
  String _encryptText = '';
  String _password = '';
  TextEditingController _encryptTextController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _encryptTextController.text = _encryptText;
    return Scaffold(
      appBar: AppBar(
        title: Text('文字加解密', style: TextStyle(fontSize: 14)),
        elevation: 0,
        backgroundColor: Global.primaryColor,
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                margin: EdgeInsets.all(15),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Global.fontColor.withOpacity(.03),
                  border: Border.all(
                    color: Global.fontColor.withOpacity(.5),
                  ),
                  borderRadius: BorderRadius.circular(5),
                ),
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxWidth: MediaQuery.of(context).size.width,
                  ),
                  child: TextField(
                    maxLines: 7,
                    style: TextStyle(
                      color: Global.fontColor,
                      fontSize: 14,
                    ),
                    decoration: InputDecoration.collapsed(
                      hintText: '请输入要加密或解密的原文，点击下方按钮进行加解密。',
                      hintStyle: TextStyle(
                        color: Global.fontSecondColor.withOpacity(0.5),
                        fontSize: 14,
                      ),
                    ),
                    onChanged: (text) {
                      setState(() {
                        _originText = text;
                      });
                    },
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(15, 1, 15, 1),
                padding: EdgeInsets.fromLTRB(5, 4, 5, 4),
                decoration: BoxDecoration(
                  color: Color(0xFFEEC747).withOpacity(0.2),
                  border: Border.all(
                    color: Color(0xFFEEC747),
                  ),
                  borderRadius: BorderRadius.circular(3),
                ),
                child: Row(
                  children: [
                    Icon(
                      IconFont.tip,
                      size: 14,
                      color: Color(0xFFEEC747),
                    ),
                    SizedBox(width: 5),
                    Text(
                      '请输入4位数字密码进行加解密',
                      style: TextStyle(
                        color: Color(0xFFA48625),
                        fontSize: 13,
                      ),
                    )
                  ],
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    margin: EdgeInsets.all(15),
                    padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      color: Global.fontColor.withOpacity(.03),
                      border: Border.all(
                        color: Global.fontColor.withOpacity(.5),
                      ),
                      borderRadius: BorderRadius.circular(5),
                    ),
                    child: ConstrainedBox(
                      constraints: BoxConstraints(
                        maxWidth: 100,
                      ),
                      child: TextField(
                        maxLines: 1,
                        style: TextStyle(
                          color: Global.fontColor,
                          fontSize: 14,
                        ),
                        inputFormatters: <TextInputFormatter>[
                          WhitelistingTextInputFormatter.digitsOnly, //仅数字密码
                          LengthLimitingTextInputFormatter(4) //限制密码长度
                        ],
                        decoration: InputDecoration.collapsed(
                          hintText: '输入密码',
                          hintStyle: TextStyle(
                            color: Global.fontSecondColor.withOpacity(0.5),
                            fontSize: 14,
                          ),
                        ),
                        onChanged: (text) {
                          setState(() {
                            _password = text;
                          });
                        },
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.all(15),
                    width: 180,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        RaisedButton(
                          color: Global.primaryColor,
                          elevation: 0,
                          child: Text(
                            '文字加密',
                            style: TextStyle(
                              color: Global.backgroundColor,
                              fontSize: 12,
                            ),
                          ),
                          onPressed: () {
                            setState(() {
                              if (_password.length < 4) {
                                Toast.show(context,
                                    msg: '请输入4位数字密码', duration: 1);
                              } else {
                                setState(() {
                                  _encryptText = EncryptUtil.aesEncode(
                                      _originText, _password);
                                });
                              }
                            });
                          },
                        ),
                        RaisedButton(
                          color: Global.primaryColor,
                          child: Text(
                            '字符解密',
                            style: TextStyle(
                              color: Global.backgroundColor,
                              fontSize: 12,
                            ),
                          ),
                          onPressed: () {
                            if (_password.length < 4) {
                              Toast.show(context,
                                  msg: '请输入4位数字密码', duration: 1);
                            } else {
                              setState(() {
                                _encryptText = EncryptUtil.aesDecode(
                                    _originText, _password);
                              });
                            }
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Container(
                margin: EdgeInsets.all(15),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Global.fontColor.withOpacity(.07),
                  border: Border.all(
                    color: Global.fontColor.withOpacity(.15),
                  ),
                  borderRadius: BorderRadius.circular(5),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    ConstrainedBox(
                      constraints: BoxConstraints(
                        maxWidth: MediaQuery.of(context).size.width,
                      ),
                      child: TextField(
                        maxLines: 15,
                        readOnly: true,
                        controller: _encryptTextController,
                        style: TextStyle(
                          color: Global.fontColor,
                          fontSize: 14,
                        ),
                        decoration: InputDecoration.collapsed(
                          hintText: '处理后的文字会在这里显示',
                          hintStyle: TextStyle(
                            color: Global.fontSecondColor.withOpacity(0.5),
                            fontSize: 14,
                          ),
                        ),
                      ),
                    ),
                    RaisedButton.icon(
                      color: Global.primaryColor,
                      elevation: 5,
                      icon: Icon(
                        IconFont.fuzhi,
                        color: Global.backgroundColor,
                        size: 20,
                      ),
                      label: Text(
                        '复制加密字符',
                        style: TextStyle(
                          color: Global.backgroundColor,
                          fontSize: 12,
                        ),
                      ),
                      onPressed: () {
                        Toast.show(context, msg: '已复制到剪贴板', duration: 1);
                        Clipboard.setData(
                            ClipboardData(text: _encryptTextController.text));
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
